namespace RL

open Raylib_CsLo
open RL.Random
open System.Numerics

type SharedState(imagePath : string, spriteWidth : int, spriteHeight : int, screenWidth : int, screenHeight : int) =
    let mutable rng = RandomNumberGenerator(MakeRandomState)

    member val QuitFlag = false with get, set
    member val RNG = rng with get

    member val SpriteSheet = SpriteAtlas(Raylib.LoadTexture(imagePath), spriteWidth, spriteHeight) with get
    member val ScreenTexture = Raylib.LoadRenderTexture(screenWidth * spriteWidth, screenHeight * spriteHeight)

    member val MapLayer = Raylib.LoadRenderTexture(screenWidth * spriteWidth, screenHeight * spriteHeight)
    member val ActorLayer = Raylib.LoadRenderTexture(screenWidth * spriteWidth, screenHeight * spriteHeight)

    member s.LoadState(state : uint64) =
        rng <- RandomNumberGenerator(state)

    member s.DrawScreenTexture =
        Raylib.DrawTexturePro( s.ScreenTexture.texture,
                               Rectangle(0F, 0F, single s.ScreenTexture.texture.width, single -s.ScreenTexture.texture.height),
                               Rectangle(0F, 0F, single(Raylib.GetScreenWidth()), single(Raylib.GetScreenHeight())),
                               Vector2.Zero,
                               0F,
                               Raylib.WHITE )

    member s.RenderLayers =
        let w = single s.ScreenTexture.texture.width
        let h = single s.ScreenTexture.texture.height

        Raylib.BeginTextureMode(s.ScreenTexture)    // Start a draw mode for the screen texture.
        Raylib.ClearBackground(Raylib.BLACK)        // Clear the screen texture's background.
        // Draw the map layer.
        Raylib.DrawTexturePro( s.MapLayer.texture,
                                Rectangle(0F, 0F, single s.MapLayer.texture.width, single -s.MapLayer.texture.height),
                                Rectangle(0F, 0F, w, h),
                                Vector2.Zero,
                                0F,
                                Raylib.WHITE )
        // Draw the actor layer.
        Raylib.DrawTexturePro( s.ActorLayer.texture,
                                Rectangle(0F, 0F, single s.ActorLayer.texture.width, single -s.ActorLayer.texture.height),
                                Rectangle(0F, 0F, w, h),
                                Vector2.Zero,
                                0F,
                                Raylib.WHITE )
        Raylib.EndTextureMode()                     // End the draw mode block.
